package com.justcan.demo.cartoon.libgdx;

import android.util.Log;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.utils.ArrayMap;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.utils.viewport.FillViewport;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;

import java.math.BigDecimal;

import javax.microedition.khronos.opengles.GL10;

/**
 * Created by justcan on 2018/1/22.
 */

public class AppMain extends ApplicationAdapter {
    private static final float MIN_SCENE_WIDTH = 800.0f;
    private static final float MIN_SCENE_HEIGHT = 600.0f;
    private static final float MAX_SCENE_WIDTH = 1280.0f;
    private static final float MAX_SCENE_HEIGHT = 800.0f;

    private Camera camera;

    private ArrayMap<String, Viewport> viewports; // 使用ViewPort相机已经在屏幕中央了
    private Viewport viewport;
    private int currentViewport;

    private MainStage mainStage;

    private InputListenEvent inputListenEvent;

    public AppMain() {
    }

    @Override
    public void create() {
        camera = new OrthographicCamera();

        Log.e("高宽--", Gdx.graphics.getWidth() + "--" + Gdx.graphics.getHeight());


        viewport = new FillViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera);
        mainStage = new MainStage(viewport);

        inputListenEvent = new InputListenEvent(); // 初始化监听事件

        Gdx.input.setInputProcessor(mainStage);
        Gdx.input.setInputProcessor(inputListenEvent);
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        mainStage.act();
        mainStage.draw();
    }

    @Override
    public void resize(int width, int height) {
        viewport.update(width, height, true); // centerCamera=true将camera设置在world的中央
    }

    private void createViewports() {
        viewports = new ArrayMap<String, Viewport>();
        viewports.put("StretchViewport", new StretchViewport(MIN_SCENE_WIDTH, MIN_SCENE_HEIGHT, camera));
//      上面初始化和下面效果一致
//      viewports.put("StretchViewport", new ScalingViewport(Scaling.stretch, MIN_SCENE_WIDTH, MIN_SCENE_HEIGHT, camera));
        viewports.put("FitViewport", new FitViewport(MIN_SCENE_WIDTH, MIN_SCENE_HEIGHT, camera));
//      上面初始化和下面效果一致
//      viewports.put("FitViewport", new ScalingViewport(Scaling.fit, MIN_SCENE_WIDTH,MIN_SCENE_HEIGHT, camera));
        viewports.put("FillViewport", new FillViewport(MIN_SCENE_WIDTH, MIN_SCENE_HEIGHT, camera));
//      上面初始化和下面效果一致
//      viewports.put("FillViewport", new ScalingViewport(Scaling.fill, MIN_SCENE_WIDTH, MIN_SCENE_HEIGHT, camera));
        viewports.put("ScreenViewport", new ScreenViewport(camera));
        viewports.put("ExtendViewport (no max)", new ExtendViewport(MIN_SCENE_WIDTH, MIN_SCENE_HEIGHT, camera));
        viewports.put("ExtendViewport (max)", new ExtendViewport(MIN_SCENE_WIDTH, MIN_SCENE_HEIGHT, MAX_SCENE_HEIGHT,
                MAX_SCENE_WIDTH, camera));

        currentViewport = -1;
    }

    private void selectNextViewport() {
        currentViewport = (currentViewport + 1) % viewports.size;
        Viewport viewport = viewports.getValueAt(currentViewport);
        viewport.apply(true);  // 将camera至于屏幕中央

        viewport.update(Gdx.graphics.getWidth(),
                Gdx.graphics.getHeight(), true);  // centerCamera=true将camera设置在world的中央

        mainStage.setViewport(viewport);

    }

    class InputListenEvent extends InputAdapter {
        // 可以参考上一节将的内容
        // 点击屏幕自动切换Viewport
        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
//            selectNextViewport();
            return false;
        }

    }
}
